[马上结贴]ACCESS 数据库 查询最后一个ID(自动编号)

来源:百度知道 编辑:UC知道 时间:2024/09/21 20:18:03
myconn.Open();
string sel="select ID from pay";
OleDbCommand comm = new OleDbCommand(sel,myconn);
object o = comm.ExecuteScaler(comm);
this.Label1.Text = o.ToString();

我的意思是想得到最后一个ID的自动编号的值
谁能帮我修改以下代码,最后Label得到返回值

SQL Server中:

SELECT @@IDENTITY AS 'ID'

以上语句可得最后插入的ID

ACCESS中:

没有相应功能!
重新查询一次获取
select id from pay where id=(select top 1 id from pay order by id desc)

你试试改成这样

myconn.Open();
string sel="select max(ID) from pay";
OleDbCommand comm = new OleDbCommand(sel,myconn);
OleDBDataReader o = comm.ExecuteNonQuery(comm);
if(o.read())
this.Label1.Text = o[对应的列].ToString();

用max函数看看,id因该是4位整数吧?友情帮顶
myconn.Open();
string sel="select max(ID) from pay";
OleDbCommand comm = new OleDbCommand(sel,myconn);
object o = comm.ExecuteScaler(comm);
this.Label1.Text = o.ToString();

我对ASP.net不熟悉,你想得最后一个ID,把纪录按降序排列不就OK了??
select top1 id from pay order by id desc

select max(id) from pay....
多了解系统函数很重要!

用VALUE试一试!!